package com.p2p.cms.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.p2p.base.dao.BaseMapper;

/**
 * 
 * 
 * @author fansunion@qq.com 2014年11月3日
 */
public interface CategoryDao extends BaseMapper {

	String TABLE_NAME = "p2p_category";
	String STATUS_REMOVED = "1";
	String STATUS_NORMAL = "0";

	/***************** 查询 ******************/
	@Select("select * from " + TABLE_NAME + " where id = #{id}")
	Map<String, Object> get(Integer id);

	@Select("select * from " + TABLE_NAME + " where  status =  " + STATUS_NORMAL
			+ " order by sort desc,id desc")
	List<Map<String, Object>> listAllForEdit();
	
	@Select("select * from " + TABLE_NAME + " where parent_id=-1 and status =  " + STATUS_NORMAL
			+ " order by sort desc,id desc")
	List<Map<String, Object>> listAllParent();

	@Select("select * from " + TABLE_NAME + " where  parent_id != -1  and status =  " + STATUS_NORMAL
			+ " order by sort desc,id desc")
	List<Map<String, Object>> listAllChild();
	
	// 暂时用不到
	@Deprecated
	@Select("select id,count(*) count from " + TABLE_NAME
			+ " where status=0 group by id")
	List<Map<String, Object>> listAllCount();

	/***************** 修改 ******************/
	@Update("update " + TABLE_NAME + " set update_time = now(), "
			+ " status = " + STATUS_REMOVED + " where id=#{id}")
	void remove(Integer id);

	void add(Map<String, Object> params);

	void update(Map<String, Object> params);

	@Select("select count(*) from p2p_post where category_id = #{id} and status= 0")
	Integer getCount(Integer id);
	
	@Select("select count(*) from p2p_post where category_id in (#{idList}) and status= 0")
	Integer getCountIn(String idList);

	@Select("select id from p2p_category where parent_id = #{id} and status = 0")
	List<Integer> getChildIdList(Integer id);
	
	@Select("select id,name from p2p_category where parent_id = #{id} and status = 0")
	List<Map<String,Object>> getChildSimpleList(Integer id);



}
